#include "Soldier.h"


Soldier::Soldier()
{
	m_IdImage = IDImage::IMG_SoldierRun;
	m_Sprite = Source::getInstance()->getSprite(m_IdImage);
	m_Position.x = 150;
	m_Position.y = GROUND_Y - 10;
	m_Vpx = 0;
	m_Vpy = VIEW_PORT_Y;
	m_Flip = SpriteEffect::None;
	m_TimeAnimation = 2.5 * ((float)1 / FPS);

	m_TimeJump = 0;

	m_Gametime = new GameTime();

	m_Veclocity_vx = SPEED;

	m_Status = IDStatus::FALL_DOWN;

	m_CheckCollision = new Collision();
}

void Soldier::MoveRight()
{
	m_Veclocity_vx = SPEED;
	m_Flip = SpriteEffect::None;
}

void Soldier::MoveLeft()
{
	m_Veclocity_vx = -SPEED;
	m_Flip = SpriteEffect::Flip;
}

void Soldier::Jump()
{
	if (m_Status == IDStatus::JUMP_UP || m_Status == IDStatus::AFTER_JUMP)
	{
		m_TimeJump -= m_TimeAnimation;
		m_Sprite = Source::getInstance()->getSprite(IDImage::IMG_SoldierJump);
		m_Sprite->Next();

		if (m_TimeJump <= 0)
		{
			m_Status = IDStatus::AFTER_JUMP;
			m_Veclocity_vy = 0.0f;
		}
		else
			m_Position.y += m_Veclocity_vy;
	}
	else
	{
		m_Sprite = Source::getInstance()->getSprite(m_IdImage);
	}
}

void Soldier::Update()
{
	m_Gametime->setTime();
	if (m_Gametime->getElapsedTimePerSecond() >= m_TimeAnimation)
	{
		m_Gametime->Update();
		m_Sprite->Next();
		
		m_Position.x += m_Veclocity_vx;
		m_Position.y += m_Veclocity_vy;

		if (m_Position.x + m_Sprite->getWidth() > SCREEN_WIDTH && m_Status == IDStatus::STAND )
		{
			MoveLeft();
		}

		if (m_Position.x < 0 && m_Status == IDStatus::STAND || m_Status == IDStatus::FALL_DOWN)
		{
			MoveRight();
		}

		int n;
		n = rand() % 4;

		if (!m_IsCollision)
		{
			if (m_TimeJump == 0.0f && m_Status == IDStatus::FALL_DOWN)
			{
				m_TimeJump = 5 * m_TimeAnimation;
				m_Veclocity_vy = VECLOCITY_VY;

				m_Status = IDStatus::JUMP_UP;
			}
		}

		Jump();

		if (m_Status != IDStatus::JUMP_UP)
			m_Position.y += m_Gravity;

		// update lai trong luc
		m_Gravity = -FALLDOWN_VELOCITY_DECREASE;
	}
}

void Soldier::UpdateCollisionMap(vector<ObjectTittle> &NodeTitle)
{
	m_Box = getBouding();
	
	m_Box.vy += m_Gravity;// bi gia toc trong truong keo xuong
	m_Box.height = 2;// gia han chieu cao

	if (m_Flip == SpriteEffect::None)
	{
		m_Box.x += (float)m_Sprite->getWidth() * 3 / 4;
		m_Box.width -= (float)m_Sprite->getWidth() / 4;
	}
	else
	{
		m_Box.width -= (float)m_Sprite->getWidth() / 4;
	}

	Character::UpdateCollisionMap(NodeTitle);
}

Soldier::~Soldier()
{
}
